package com.example.mybatisplus.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.DutyTimeMapper;
import com.example.mybatisplus.mapper.FirstApoStatusMapper;
import com.example.mybatisplus.model.domain.DutyTime;
import com.example.mybatisplus.model.domain.FirstAppointment;
import com.example.mybatisplus.service.FirstAppointmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.model.domain.FirstApoStatus;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.FirstApoStatusService;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author cft
 * @since 2022-06-27
 * @version v1.0
 */
@Controller
@RequestMapping("/api/firstApoStatus")
public class FirstApoStatusController {

	private final Logger logger = LoggerFactory.getLogger(FirstApoStatusController.class);

	@Autowired
	private FirstApoStatusService firstApoStatusService;

	/**
	* 描述：根据Id 查询
	*
	*/
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResponse getById(@PathVariable("id") Long id) throws Exception {
		FirstApoStatus firstApoStatus = firstApoStatusService.getById(id);
		return JsonResponse.success(firstApoStatus);
	}

	/**
	* 描述：根据Id删除
	*
	*/
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
		firstApoStatusService.removeById(id);
		return JsonResponse.success(null);
	}

	/**
	* 描述：根据Id 更新
	*
	*/
	@RequestMapping(value = "", method = RequestMethod.PUT)
	@ResponseBody
	public JsonResponse updateFirstApoStatus(FirstApoStatus firstApoStatus) throws Exception {
		firstApoStatusService.updateById(firstApoStatus);
		return JsonResponse.success(null);
	}

	/**
	* 描述:创建FirstApoStatus
	*
	*/
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	public JsonResponse create(FirstApoStatus firstApoStatus) throws Exception {
		firstApoStatusService.save(firstApoStatus);
		return JsonResponse.success(null);
	}

	/*
	* author:tqq
	* 查询所有需要安排咨询且还未被咨询助理安排的初访结果
	* */
	@GetMapping("/list")
	@ResponseBody
	public JsonResponse list(PageDTO pageDTO) {
		Page<FirstApoStatus> page = firstApoStatusService.dispayFapoRecord(pageDTO);
		return JsonResponse.success(page);
	}

	/*
	 * author:cft
	 * function:新增初访状态
	 */
	@RequestMapping("/newFApo")
	@ResponseBody
	public JsonResponse newFApo(@RequestBody JSONObject jsonObject) {
		FirstApoStatus firstApoStatus = new FirstApoStatus();
		firstApoStatus.setFApoId(Long.parseLong(jsonObject.getString("fApoId")))
		.setFPlace(jsonObject.getString("fPlace"))
		.setFSelecPeriod(Integer.parseInt(jsonObject.getString("fSelecPeriod")))
		.setFPriority(Integer.parseInt(jsonObject.getString("fPriority")))
				.setFViewerId(Long.parseLong(jsonObject.getString("fViewerId")));
		firstApoStatusService.save(firstApoStatus);
		return JsonResponse.success("success");
	}


    /*
     * author:jrh
     * function:查询初访记录信息
     */

    @RequestMapping("/getFirstInfo")
    @ResponseBody
    public JsonResponse getFDuty(){
        List<FirstApoStatus> list=firstApoStatusService.getFInfo();
        return JsonResponse.success(list);
    }

    /*
     * author:jrh
     * function:初访员填写初访记录初访记录信息
     */

    @RequestMapping("/update")
    @ResponseBody
    public JsonResponse update(@RequestBody JSONObject jsonObject){
        FirstApoStatus firstApoStatus=new FirstApoStatus();
        firstApoStatus.setFViewId(Long.parseLong(jsonObject.getString("fViewId")))
                .setFApoRecord(jsonObject.getString("fApoRecord")).setFResult(jsonObject.getString("fResult"));
        firstApoStatusService.updateById(firstApoStatus);
        return JsonResponse.success(11);
    }


	/*
	 * author:tqq
	 * function:根据当前登录学生查询初访结果信息
	 */
	@GetMapping("/getFirstApoResultByStuId")
	@ResponseBody
	public JsonResponse getFirstApoResultByStuId() {
		List<FirstApoStatus> list = firstApoStatusService.getBystuId(SessionUtils.getCurrentUserInfo().getUserId());
		return JsonResponse.success(list);
	}
	/*
	 * author:jrh
	 * function:根据初访序号查询对应初访地点
	 */
	@Autowired
	FirstApoStatusMapper firstApoStatusMapper;
	@PostMapping("/getPlace")
	@ResponseBody
	public JsonResponse getPlace(@RequestBody JSONObject jsonObject) {
		Long id=Long.parseLong(jsonObject.getString("fapoId"));
		List<FirstApoStatus> list=firstApoStatusMapper.getPlace(id);
		return JsonResponse.success(list);
	}

}


